package com.teach;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.List;

import com.teach.util.FileUtil.LineReadListener;

import cn.hutool.core.io.FileUtil;

/**
 * <pre>
 * - Reader 读取流 
 * Author : J.L.Zhou
 * E-Mail : 2233875735@qq.com
 * Tel : 151 1104 7708
 * Date : 2021-4-26 14:04:42
 * Version : 1.0
 * Copyright 2021 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 */
public class Demo01 {

	public static void main(String[] args) throws Exception {
		File file = new File("d:/upload/in.txt");
		try (
				Reader reader = new FileReader(file);
		) {
//			int temp = reader.read();
//			System.out.println(temp);
			char[] cs = new char[1024];
			int len = 0;
			while ((len = reader.read(cs)) > -1) {
				System.out.println(len + Arrays.toString(cs));
			}

		}
		
		System.out.println("--------------------------------------");

		try (
				Reader reader = new FileReader(file);
				BufferedReader bufferedReader = new BufferedReader(reader);
		) {
			String line = null;
			int num = 1;
			while((line = bufferedReader.readLine()) != null) {
				System.out.println(num+++":"+line);
			}

		}
		
		System.out.println("--------------------------------------");
		file = new File("d:/upload/out1.txt");
		try (
				InputStream in = new FileInputStream(file);
//				Reader reader = new InputStreamReader(in);//默认采用UTF-8
				Reader reader = new InputStreamReader(in,"GBK");
//				Reader reader = new InputStreamReader(in,Charset.forName("GBK"));
				BufferedReader bufferedReader = new BufferedReader(reader);
		) {
			String line = null;
			int num = 1;
			while((line = bufferedReader.readLine()) != null) {
				System.out.println(num+++":"+line);
			}

		}
		
		System.out.println("---------------------使用自己的工具类");
		String str = com.teach.util.FileUtil.readText(file, "GBK");
		System.out.println(str);
		com.teach.util.FileUtil.readText(file, "UTF-8", (line)-> {
				System.out.println(line);
		});
		
		System.out.println("---------------------使用hutool的工具类");
		str = FileUtil.readString(file, "GBK");
		System.out.println(str);
		List<String> list = FileUtil.readLines(file, "gbk");
		System.out.println(list);

		
	}
}
